//
//  AppDelegate.m
//  StormWatch
//
//  Created by 王岩 on 2024/8/29.
//

#import "AppDelegate.h"
#import "TabbarViewController.h"
#import <CocoaLumberjack/CocoaLumberjack.h>
@interface AppDelegate ()
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    TabbarViewController * tabbar = [[TabbarViewController alloc] init];
    
    self.window.rootViewController = tabbar;
    
    [self.window makeKeyAndVisible];
    
    [DDLog addLogger:[DDOSLogger sharedInstance]];
    DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
    //重用log文件，不要每次启动都创建新的log文件(默认值是NO)
    fileLogger.doNotReuseLogFiles = NO;
    //log文件在24小时内有效，超过时间创建新log文件(默认值是24小时)
    fileLogger.rollingFrequency = 60*60*24;
    //log文件的最大3M(默认值1M)
    fileLogger.maximumFileSize = 1024*1024*3;
    //最多保存7个log文件(默认值是5)
    fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
    //log文件夹最多保存10M(默认值是20M)
    fileLogger.logFileManager.logFilesDiskQuota = 1014*1024*20;
    //添加文件写入logger
    [DDLog addLogger:fileLogger];

    return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    
    self.taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"com.frq.background_bluetoothTask" expirationHandler:^{
        
        [[UIApplication sharedApplication] endBackgroundTask:self.taskIdentifier];
        self.taskIdentifier = UIBackgroundTaskInvalid;
    }];
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    
    [[UIApplication sharedApplication] endBackgroundTask:self.taskIdentifier];
    self.taskIdentifier = UIBackgroundTaskInvalid;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    TabbarViewController * tabbar = (TabbarViewController *)self.window.rootViewController;
    if (tabbar.selectedIndex > 1) {
        return;
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"appDidBecomeActive" object:nil];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    
    [[FRQBluetoothManager shareManager] closeAllPeripheralsConnection:^(id<FRBleAbility>  _Nonnull ability) {
        
    }];
}
@end
